<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/17 0017
 * Time: 下午 1:49
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Content extends BasicAdmin
{
    public $table = 'Content';
    public function index(){
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['content'] = json_encode($post['content'],320);
            DataService::save($this->table, $post);
            $this->success('保存成功！', '');
        }
        $id = $this->request->get('id');
        $title = Db::name('CategoryModule')->field('name,template')->where('id',$id)->find();
        $db = Db::name($this->table)->where('pid',$id)->find();
        $db['content'] = json_decode($db['content'],true);
        $this->assign([
            'title'=>$title['name'],
            'vo'=>$db,
            'pid'=>$id
        ]);
        return view($title['template']);
    }

    public function lists(){
        $id = $this->request->get('id');
        $cate = Db::name('CategoryModule')->field('name,template')->where('id',$id)->find();
        $db = Db::name($this->table)->where(['pid'=>$id,'is_deleted'=>1])->order('sort asc,id asc');
        $this->assign([
            'title'=>$cate['name'],
            'pid'=>$id
        ]);
        return parent::_list($db,true,true,false,[],$cate['template']);
    }

    public function _lists_data_filter(&$data){
        foreach ($data as &$v){
            $v['content'] = json_decode($v['content'], true);
        }
    }

    public function lists_add(){
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['content'] = json_encode($post['content'],320);
            DataService::save($this->table, $post);
            $this->success('保存成功！', '');
        }
        $id = $this->request->get('id');
        $vo = Db::name($this->table)->where('id',$id)->find();

        $pid = $this->request->get('pid') ?: $vo['pid'];
        $cate = Db::name('CategoryModule')->field('name,template')->where('id',$pid)->find();
        $vo['content'] = json_decode($vo['content'],true);
        $this->assign([
            'pid'=>$pid,
            'title'=>$cate['name'] . ' - ' . '添加/编辑',
            'vo'=>$vo
        ]);
        return view($cate['template'] . '_add');
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lists_forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lists_resume()
    {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lists_del()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}